//
//  GroupContractsViewController.m
//  Findif
//
//  Created by Mac on 2019/3/5.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "GroupContractsViewController.h"
#import "AddGroupTableViewCell.h"
#import "GroupModel.h"

@interface GroupContractsViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray <GroupModel *> * listArray;

@end

@implementation GroupContractsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    [self loadData];
    [self setupNav];
    [self setupUI];
}


- (void)setupNav{
    
    self.title = @"我的社团";
    
    UIButton * backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35 * SCREENH6, 15 * SCREENH6)];
    
    [backButton setTitle:@"取消" forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToView) forControlEvents:UIControlEventTouchUpInside];
    backButton.titleLabel.font = [UIFont boldSystemFontOfSize:15 * SCREENH6];
    [backButton setTitleColor:ZRBlackColor forState:UIControlStateNormal];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
}

- (void)returnGroupName:(SelectedGroupBlock)block{
    
    self.selectedGroupBlock = block;
}


- (void)setListArray:(NSArray<GroupModel *> *)listArray{
    
    _listArray = listArray;
    
    [self.tableView reloadData];
}


- (void)loadData{
    
    NSString * user_id = DEF_GET_OBJECT(@"id");
    
    [[ZRNetworkTool sharedTool] getUserGroupListWithUser_idt:user_id WithBlock:^(BOOL success, id responseData) {
        
        if (success) {
            
            self.listArray = [NSArray yy_modelArrayWithClass:[GroupModel class] json:responseData[@"data"]];
        }
    }];
}



- (void)setupUI{
    
    self.view.backgroundColor = ColorWithRGB(0xF4F5F6);
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = ColorWithRGB(0xF4F5F6);
        tableView.rowHeight = 60 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.showsVerticalScrollIndicator = NO;
//    if (@available(iOS 11.0, *)) {
//        tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
//    } else {
//        self.automaticallyAdjustsScrollViewInsets = NO;
//    }
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[AddGroupTableViewCell class] forCellReuseIdentifier:@"PushSquareGroupTableViewCell"];
    
    //    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
    //
    //        [self loadData];
    //    }];
    
    [self.view addSubview:self.tableView];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.listArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    AddGroupTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PushSquareGroupTableViewCell" forIndexPath:indexPath];
    
    cell.pushSquareModel = self.listArray[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if([self.type isEqualToString:@"1"]){
        
        GroupModel * model = self.listArray[indexPath.row];
        
        NSString * ID = [NSString stringWithFormat:@"%@",model.group_id];
        NSDictionary * dict = @{@"ID":ID};
        
        self.selectedGroupBlock(dict);
        
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        
        GroupModel * model = self.listArray[indexPath.row];
        
        NSString * ID = [NSString stringWithFormat:@"%@",model.group_id];
        NSString * name = [NSString stringWithFormat:@"%@",model.group_name];
        NSDictionary * dict = @{@"ID":ID,@"name":name};
        
        self.selectedGroupBlock(dict);
        
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}


- (void)backToView{
    
    if([self.type isEqualToString:@"1"]){
        
        
    }else{
        
        if (self.selectedGroupBlock != nil) {
            
            NSDictionary * dict = @{@"name":@"所在社团"};
            
            self.selectedGroupBlock(dict);
        }
    }
    
    
    [self.navigationController popViewControllerAnimated:YES];
}


@end
